﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using Elf.Data.Entity.Interfaces;

namespace Elf.Data.Entity
{
    public static class ContextBaseExtensions
    {
        /// <summary>
        /// Adds entity as it is modified.
        /// </summary>
        /// <typeparam name="TEntity">The type of the entity.</typeparam>
        /// <param name="context">The context.</param>
        /// <param name="entity">The entity.</param>
        public static void EntryAsModified<TEntity>(this DbContext context, TEntity entity) 
            where TEntity : class, IEntityBase
        {
            if (context == null) throw new ArgumentNullException("context");
            if (entity == null) throw new ArgumentNullException("entity");

            context.Entry(entity).State = EntityState.Modified;
        }

        /// <summary>
        /// Adds entities as they are modified.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="context">The context.</param>
        /// <param name="entities">The entities.</param>
        public static void EntriesAsModified<TEntity>(this DbContext context, IEnumerable<TEntity> entities)
            where TEntity : class, IEntityBase
        {
            if (context == null) throw new ArgumentNullException("context");
            if (entities == null) throw new ArgumentNullException("entities");

            foreach (var entity in entities)
            {
                context.Entry(entity).State = EntityState.Modified;
            }
        }
    }

}
